#include <sdk.h>
#include <prep.h>
#include <wx/tglbtn.h>
#include "compilerflagdlg.h"
#include "compileroptions.h"

CompilerFlagDlg::CompilerFlagDlg( wxWindow* parent, CompOption* opt, wxArrayString& categ, const wxString &selectedCategory ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/Compiler_Flag.xrc" );
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "CompilerFlagDlg" ), _T( "wxDialog" ) ) ) {
    return;
  }
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  NameText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Name" ) );
  CompilerText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Compiler" ) );
  LinkerText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Linker" ) );
  CategoryCombo = ( wxComboBox* )FindWindow( XRCID( "ID_Category" ) );
  AgainstText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Against" ) );
  MessageText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Message" ) );
  SupersedeText = ( wxTextCtrl* )FindWindow( XRCID( "ID_Supersede" ) );
  ExclusiveToggle = ( wxToggleButton* )FindWindow( XRCID( "ID_Exclusive" ) );
  Connect( XRCID( "ID_Against" ), wxEVT_COMMAND_TEXT_UPDATED, ( wxObjectEventFunction )&CompilerFlagDlg::OnAdvancedOption );
  Connect( XRCID( "ID_Exclusive" ), wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, ( wxObjectEventFunction )&CompilerFlagDlg::OnAdvancedOption );
  copt = opt;
  if( !copt->name.IsEmpty() ) {
    SetTitle( _T( "Modify flag" ) );
    wxString oName;
    copt->name.EndsWith( _T( "[" ) + copt->option + _T( "]" ), &oName );
    if( oName == wxEmptyString ) {
      copt->name.EndsWith( _T( "[" ) + copt->additionalLibs + _T( "]" ), &oName );
    }
    if( oName == wxEmptyString ) {
      oName = copt->name;
    }
    NameText->SetValue( oName.Trim() );
    CompilerText->SetValue( copt->option );
    LinkerText->SetValue( copt->additionalLibs );
    CategoryCombo->SetValue( copt->category );
    AgainstText->SetValue( copt->checkAgainst );
    MessageText->SetValue( copt->checkMessage );
    SupersedeText->SetValue( copt->supersedes );
    if( copt->exclusive ) {
      ExclusiveToggle->SetValue( true );
      ExclusiveToggle->SetLabel( _T( "True" ) );
    }
  }
  CategoryCombo->Append( categ );
  if( !selectedCategory.empty() ) {
    CategoryCombo->SetStringSelection( selectedCategory );
  }
  MessageText->Enable( !AgainstText->GetValue().Trim().Trim( false ).IsEmpty() );
  SetMaxSize( wxSize( -1, GetMinHeight() ) );
}

CompilerFlagDlg::~CompilerFlagDlg() {
}

void CompilerFlagDlg::OnAdvancedOption( M_Code_unused wxCommandEvent& event ) {
  if( ExclusiveToggle->GetValue() ) {
    ExclusiveToggle->SetLabel( _T( "True" ) );
  } else {
    ExclusiveToggle->SetLabel( _T( "False" ) );
  }
  MessageText->Enable( !AgainstText->GetValue().Trim().Trim( false ).IsEmpty() );
}

void CompilerFlagDlg::EndModal( int retCode ) {
  if( retCode == wxID_CANCEL ) {
    wxDialog::EndModal( retCode );
    return;
  }
  bool same = copt->name.BeforeLast( _T( '[' ) ).Trim() == NameText->GetValue() &&
              copt->option == CompilerText->GetValue()  && copt->additionalLibs == LinkerText->GetValue() &&
              copt->category == CategoryCombo->GetValue() && copt->checkAgainst == AgainstText->GetValue() &&
              copt->checkMessage == MessageText->GetValue() && copt->supersedes == SupersedeText->GetValue() &&
              ( copt->exclusive == ExclusiveToggle->GetValue() );
  copt->name = NameText->GetValue().Trim().Trim( false );
  copt->option = CompilerText->GetValue().Trim().Trim( false );
  copt->additionalLibs = LinkerText->GetValue().Trim().Trim( false );
  copt->category = CategoryCombo->GetValue().Trim().Trim( false );
  if( copt->category.IsEmpty() ) {
    copt->category = _T( "General" );
  }
  copt->checkAgainst = AgainstText->GetValue().Trim().Trim( false );
  if( !copt->checkAgainst.IsEmpty() ) {
    copt->checkMessage = MessageText->GetValue().Trim().Trim( false );
  }
  copt->supersedes = SupersedeText->GetValue().Trim().Trim( false );
  copt->exclusive  = ExclusiveToggle->GetValue();
  if( same || copt->name.IsEmpty() || ( copt->option.IsEmpty() && copt->additionalLibs.IsEmpty() ) ) {
    wxDialog::EndModal( wxID_CANCEL );
  } else {
    wxDialog::EndModal( retCode );
  }
}
